#include "OgreStableHeaders.h"
#include "OgreHardwareIndexBuffer.h"
#include "OgreHardwareBufferManager.h"
#include "OgreDefaultHardwareBufferManager.h"

namespace Ogre
{
	HardwareIndexBuffer::HardwareIndexBuffer(HardwareBufferManagerBase* mgr, IndexType idxType,
		size_t numIndexes, HardwareBuffer::Usage usage,
		bool useSystemMemory, bool useShadowBuffer)
		: HardwareBuffer(usage, useSystemMemory, useShadowBuffer)
		, mMgr(mgr)
		, mIndexType(idxType)
		, mNumIndexes(numIndexes)
	{
		switch(mIndexType)
		{
		case IT_16BIT:
			mIndexSize = sizeof(unsigned short);
			break;
		case IT_32BIT:
			mIndexSize = sizeof(unsigned int);
			break;
		}
		mSizeInBytes = mIndexSize * mNumIndexes;

		if (mUseShadowBuffer)
		{
			mpShadowBuffer = OGRE_NEW DefaultHardwareIndexBuffer(mIndexType,
				mNumIndexes, HardwareBuffer::HBU_DYNAMIC, mMgr);
		}
	}

	HardwareIndexBuffer::~HardwareIndexBuffer()
	{
		if (mMgr)
		{
			mMgr->_notifyIndexBufferDestroyed(this);
		}

		if (mpShadowBuffer)
		{
			OGRE_DELETE mpShadowBuffer;
		}
	}

	HardwareIndexBufferSharedBuffer::HardwareIndexBufferSharedBuffer(HardwareIndexBuffer* buf)
		: SharedPtr<HardwareIndexBuffer>(buf)
	{

	}
}
